
#region Imported Namespaces
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

using PHSRAG.Insight21.SearchContext;
using Constants = PHSRAG.Insight21.InsightUtilities.Constants;
using DataAccess = PHSRAG.Utility.DataAccess;
using PHSRAG.WebControls;
using PHSRAG.Insight21.Policy;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// Summary description for AgreementCommonSearch.
	/// </summary>

	[ToolboxData("<{0}:AgreementCommonSearch runat=server />"), Designer(typeof(AgreementCommonSearchDesigner))]
	public class AgreementCommonSearch : CommonSearch
	{
		#region Constants
		private const string ViewChangeCommand = "ViewChange";
		private const string ViewOptionKey = "viewOption_5817750F-DBAB-4436-8645-4A69AA3D2C2C";
		#endregion

		#region Private Types
		/// <summary>
		/// The ViewType enumeration MUST match the sequence in which the radio buttons are declared.
		/// </summary>
		private enum ViewType
		{
			ViewAll, ViewNIH, ViewOtherFederal, ViewIndustryCorporate, ViewNotForProfit, ViewSundries
		}
		#endregion

		#region Instance Variables
		private ICachePolicy	pageCachePolicy;
		private RadioButtonList	viewingOption;
		#endregion

		#region Private Properties
		/// <summary>
		/// Get/Set the ViewType based on the viewing option selected.
		/// </summary>
		private ViewType ViewOption
		{
			get 
			{
				object o = pageCachePolicy.GetCachedObject(ViewOptionKey); 
				return (o == null) ? ViewType.ViewAll : (ViewType)o;
			}
			set 
			{
				pageCachePolicy.Cache(ViewOptionKey, value);
			}
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Retrieve the page-cache-policy to store current viewing option. This SHOULD NOT be done on load since the
		/// on-load handler fires AFTER the current method is invoked by the parent on postback.
		/// Create a radio button list control to display various viewing options. Each radio button in the list is
		/// bound to a view description and an associated value.
		/// </summary>
		protected override void CreateAdditionalControls()
		{
			pageCachePolicy = (ICachePolicy)Page.Session[Policy.Names.PageCachePolicyKey];

            viewingOption = new RadioButtonList();
			viewingOption.ID = Constants.ViewingOptionKey;
			viewingOption.AutoPostBack = false;
			viewingOption.RepeatDirection = RepeatDirection.Horizontal;
			viewingOption.RepeatLayout = RepeatLayout.Table;
			viewingOption.CssClass = "RadioButtonList";
			viewingOption.SelectedIndex = (int)ViewOption;
			viewingOption.SelectedIndexChanged += new EventHandler(OnViewingOptionChanged);
			viewingOption.Attributes["Label"] = "Viewing Option";

			DataTable views = new DataTable();
			views.Columns.Add("ViewName");
			views.Columns.Add("ViewValue");

			DataRow row = views.NewRow();
			row["ViewName"] = "View All";
			row["ViewValue"] = (int)ViewType.ViewAll;
			views.Rows.Add(row);

			row = views.NewRow();
			row["ViewName"] = "View NIH";
			row["ViewValue"] = (int)ViewType.ViewNIH;
			views.Rows.Add(row);

			row = views.NewRow();
			row["ViewName"] = "View Other Federal";
			row["ViewValue"] = (int)ViewType.ViewOtherFederal;
			views.Rows.Add(row);

			row = views.NewRow();
			row["ViewName"] = "View Industry/Corporate";
			row["ViewValue"] = (int)ViewType.ViewIndustryCorporate;
			views.Rows.Add(row);

			row = views.NewRow();
			row["ViewName"] = "View Not For Profit";
			row["ViewValue"] = (int)ViewType.ViewNotForProfit;
			views.Rows.Add(row);

			row = views.NewRow();
			row["ViewName"] = "View Sundries";
			row["ViewValue"] = (int)ViewType.ViewSundries;
			views.Rows.Add(row);

			viewingOption.DataTextField = "ViewName";
			viewingOption.DataValueField = "ViewValue";
			viewingOption.DataSource = views;
			viewingOption.DataBind();
		}

		/// <summary>
		/// Add the radion button list for viewing options to the controls collection. This will render the radio buttons
		/// to be displayed just below the controls defined by the base common search class.
		/// </summary>
		protected override void GenerateAdditionalControlTree()
		{
			Controls.Add(viewingOption);
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Handler for the event raised by the RadioButtonList when a viewing option is changed.
		/// Store the current selection (so that state may be restored on re-visit to the page). If an event handler has
		/// been registered by the containing entity, invoke it providing the description of the selection and the numeric
		/// value associated with that selection.
		/// </summary>
		/// <param name="sender">Control which raised the event</param>
		/// <param name="e">Argument associated with the event</param>
		private void OnViewingOptionChanged(object sender, EventArgs e)
		{
			int viewOption = Convert.ToInt32(viewingOption.SelectedItem.Value);
			ViewOption = (ViewType)viewOption;

			CommandEventHandler handler = (CommandEventHandler)Events[ViewChangeCommand];
			if (handler != null)
				handler(this, new CommandEventArgs(viewingOption.SelectedItem.Text, viewOption));
		}
		#endregion
	}

	#region Class PagerDesigner
	/// <summary>
	/// Designer associated with the CommonSearch control.
	/// </summary>
	/// <remarks>
	/// This class cannot be used directly.
	/// </remarks>
	public class AgreementCommonSearchDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the CommonSearch control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			return CreatePlaceHolderDesignTimeHtml();
		}
		#endregion
	}
	#endregion
}

// --- EOF ---
